﻿using System;
using System.Data;
using System.Windows.Forms;
using CuteSprite.Eme.Configuration;
using HmFramework;
using HmFramework.UI.Utils;
using HmFramework.Security;
using ResManagerUtils = CuteSprite.Eme.UI.Utils.Resources.ResManager;

namespace CuteSprite.Eme.Server.Configuration
{
	public partial class FrmUser : HmBaseForm
	{
		private readonly String m_emptyPwd = "0        ";

		#region -- 属性 --

		private AdminConfigInfo _UserSettings = null;

		internal AdminConfigInfo UserSettings
		{
			get { return _UserSettings; }
			set { _UserSettings = value as AdminConfigInfo; }
		}

		private DataRow _UserItem = null;

		internal DataRow UserItem
		{
			get { return _UserItem; }
			set
			{
				_UserItem = value as DataRow;
				txtUser.Text = _UserItem[AdminConfigInfo._.Column_User_Name].ToString();
				txtUser.ReadOnly = true;
				//txtPwd.Text = _UserItem[AdminConfigInfo._.Column_User_Password].ToString();
				txtPwd.Text = m_emptyPwd;
			}
		}

		private Boolean _IsAddNew = true;

		/// <summary>是否新增记录</summary>
		internal Boolean IsAddNew
		{
			get { return _IsAddNew; }
			set { _IsAddNew = value; }
		}

		#endregion

		#region -- 界面初始化 --

		public FrmUser()
		{
			InitializeComponent();
			InitForm();
		}

		public static FrmUser Create(AdminConfigInfo s)
		{
			FrmUser frm = new FrmUser();
			frm.IsAddNew = true;
			frm.UserSettings = s;
			return frm;
		}

		public static FrmUser Create(AdminConfigInfo s, DataRow dr)
		{
			FrmUser frm = new FrmUser();
			frm.IsAddNew = false;
			frm.UserSettings = s;
			frm.UserItem = dr;
			return frm;
		}

		#endregion

		#region -- 窗体事件 --

		#region - method InitIcon -

		/// <summary>载入窗体图标</summary>
		protected override void InitIcon()
		{
			this.Icon = ResManagerUtils.GetIcon("logo.ico");
		}

		#endregion

		#region - method LoadLocalization -

		/// <summary>载入语言包</summary>
		protected override void LoadLocalization()
		{
			this.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmUser_Caption_From);
			btnOK.Text = UICommonLocalizer.GetString(UICommonStringId.OKButtonText);
			btnCancel.Text = UICommonLocalizer.GetString(UICommonStringId.CancelButtonText);
			gpUser.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmUser_Caption_gpUser);
			lblUser.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmUser_Caption_lblUser);
			lblPwd.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmUser_Caption_lblPwd);
		}

		#endregion

		#region - method CheckInput -

		protected override Boolean CheckInput()
		{
			String user = txtUser.Text.Trim();
			if (MatchHelper.StrIsNullOrEmpty(user))
			{
				DialogHelper.ShowWarning(this.Text, EmeConfigLocalizer.GetString(EmeConfigStringId.Message_UserEmpty));
				return false;
			}
			if (MatchHelper.StrIsNullOrEmpty(txtPwd.Text))
			{
				DialogHelper.ShowWarning(this.Text, EmeConfigLocalizer.GetString(EmeConfigStringId.Message_PwdEmpty));
				return false;
			}
			if (IsAddNew)
			{
				try
				{
					var tbUser = UserSettings.GetTable(AdminConfigInfo._.Table_User);
					// 不区分大小写
					//tbUser.CaseSensitive = false;
					var drs = tbUser.Select(AdminConfigInfo._.Column_User_Name + " = '" + user + "'");
					if (drs != null && drs.Length > 0)
					{
						DialogHelper.ShowWarning(this.Text, EmeConfigLocalizer.GetString(EmeConfigStringId.Message_UserExists).FormatWith(user));
						return false;
					}
				}
				catch { }
			}
			return true;
		}

		#endregion

		#endregion

		#region -- Events Handling --

		private void btnOK_Click(Object sender, EventArgs e)
		{
			Boolean flag = CheckInput();
			if (!flag) { return; }

			if (IsAddNew)
			{
				var tbIP = UserSettings.GetTable(AdminConfigInfo._.Table_User);
				DataRow dr = tbIP.NewRow();
				dr[AdminConfigInfo._.Column_User_ID] = Guid.NewGuid().ToString();
				dr[AdminConfigInfo._.Column_User_Name] = txtUser.Text.Trim();
				dr[AdminConfigInfo._.Column_User_Password] = DataHelper.Hash(txtPwd.Text.Trim());
				tbIP.Rows.Add(dr);
			}
			else
			{
				var pwd = txtPwd.Text;
				// 判断用户是否输入新的密码
				if (pwd != m_emptyPwd)
				{
					_UserItem[AdminConfigInfo._.Column_User_Password] = DataHelper.Hash(pwd.Trim());
				}
			}

			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void btnCancel_Click(Object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		#endregion
	}
}
